//
//  HomeTableViewController.swift
//  DSWeiBo
//
//  Created by Frank Wang on 7/6/16.
//  Copyright © 2016 Frank Wang. All rights reserved.
//

import UIKit


class HomeTableViewController: BaseTableViewController{

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
        //如果没有登录就设置为登陆界面的信息
        if userLogin == false{
                visitorView?.setupVisitorInfo(true, imageName: "visitordiscover_feed_image_house", message: "登录后，别人可以看到你")
                return  //没有登录直接return
        }
        
      setUpNav()
        
        //注册通知，监听菜单
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(HomeTableViewController.change), name: XMGPopoverAnimaterWillShow, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(HomeTableViewController.change), name: XMGPopoverAnimaterWillDismiss, object: nil)
        
    }
        
        deinit{
                NSNotificationCenter.defaultCenter().removeObserver(self)
        }
        /**
         修改标题按钮的状态
         */
        func change() {
                //修改标题按钮的状态
                let  titleBtn = navigationItem.titleView as! TitleButton
                titleBtn.selected = !titleBtn.selected
                
        }

        private func setUpNav(){
//                navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "navigationbar_friendattention", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
                
//                let leftBtn = UIButton()
//                leftBtn.setImage(UIImage(named: "navigationbar_friendattention"), forState: UIControlState.Normal)
//                leftBtn.setImage(UIImage(named: "navigationbar_friendattention_highlighted"), forState: UIControlState.Highlighted)
//                leftBtn.sizeToFit()
//                navigationItem.leftBarButtonItem = UIBarButtonItem(customView: leftBtn)
//                
//                
//                //快速改名字，Command ＋ Control ＋ e
//                let rightBtn = UIButton()
//                rightBtn.setImage(UIImage(named: "navigationbar_pop"), forState: UIControlState.Normal)
//                rightBtn.setImage(UIImage(named: "navigationbar_pop_highlighted"), forState: UIControlState.Highlighted)
//                rightBtn.sizeToFit()
//                navigationItem.leftBarButtonItem = UIBarButtonItem(customView: rightBtn)
                
                //第二种方法
//               navigationItem.leftBarButtonItem =  createBarBtnItem("navigationbar_friendattention", target: self, action: #selector(HomeTableViewController.leftItemClick))
//                
//                navigationItem.rightBarButtonItem = createBarBtnItem("navigationbar_pop", target: self, action: #selector(HomeTableViewController.rightItemClick))
                //初始化左右按钮
                navigationItem.leftBarButtonItem = UIBarButtonItem.createBarBtnItem("navigationbar_friendattention", target: self, action: #selector(HomeTableViewController.leftItemClick))
                navigationItem.rightBarButtonItem = UIBarButtonItem.createBarBtnItem("navigationbar_pop", target: self, action: #selector(HomeTableViewController.rightItemClick))
                
                //初始化标题
                let titleBtn = TitleButton()
                titleBtn.setTitle("wang ", forState: UIControlState.Normal)
                titleBtn.addTarget(self, action: #selector(HomeTableViewController.titleBtnClick), forControlEvents: UIControlEvents.TouchUpInside)
                navigationItem.titleView = titleBtn
                
        }
        
        func  titleBtnClick(btn : TitleButton){
                print(#function)
                
//                btn.selected = !btn.selected
                
                let sb = UIStoryboard(name: "PopoverViewController", bundle: nil)
                let vc = sb.instantiateInitialViewController()
                
                //设置谁来负责专场动画，专场代理
                //默认情况下Modal会移除以前控制器的View，替换为当前弹出的View
//                如果自定专场动画就会不移除之前的View
//                vc?.transitioningDelegate = self
                vc?.transitioningDelegate = popoverAnmater
                
                //设置专场的样式
                vc?.modalPresentationStyle = UIModalPresentationStyle.Custom
                presentViewController(vc!, animated: true,completion: nil)
                
        }
        
        func leftItemClick() {
                print(#function)
        }
        
        func  rightItemClick(){
//                print(#function)
                let sb = UIStoryboard(name: "QRCodeViewController", bundle: nil)
                let vc = sb.instantiateInitialViewController()

                presentViewController(vc!, animated: true, completion: nil)
                
                
        }
        
        //MARK : lazy
        //一定要定义一个属性来保存自定义转场对象，否则会报错。
        private lazy var popoverAnmater: PopoverAnimater = {
           
                let pa = PopoverAnimater()
                pa.presentFrame = CGRectMake(100, 56, 200, 200)
                return pa;
        }()
        
        
//        private func createBarBtnItem(imageName:String,target : AnyObject? ,action : Selector) -> UIBarButtonItem{
//                let Btn = UIButton()
//                Btn.setImage(UIImage(named:imageName), forState: UIControlState.Normal)
//                Btn.setImage(UIImage(named: imageName + "_highlighted"), forState: UIControlState.Highlighted)
//                Btn.sizeToFit()
//                Btn.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
//                return UIBarButtonItem(customView: Btn)
//        }
}
